import { AxiosInstance } from "axios";
import { Interceptors } from "./interceptors";

/**
 * @author  xiaoxujs
 * @time    2024-10-11
 * @title   http请求封装
 * @desc 
 *
 */

let win = window as any;

const basicIp = win.myURL.URL;
const wsIp = win.myURL.WSURL;
const saomaIp = win.myURL.SMURL;

class HttpService {
  public axios: AxiosInstance;
  constructor() {
    this.axios = new Interceptors().getInterceptors();
  }

  /**
   * get请求
   * @param params  参数
   * @param operation     接口
   */
  public getData(operation: string, params?: any) {
    return new Promise((resolve, reject) => {
      this.axios
        .get(operation, {
          params
        })
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err.message);
        });
    });
  }
  public deleteData(operation: string, params: object) {
    return new Promise((resolve, reject) => {
      this.axios
        .delete(operation, {
          params: params,
          //   paramsSerializer(params) {
          //     return qs.stringify(params, { indices: false });
          //   },
        })
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err.message);
        });
    });
  }
  /**
   * post请求
   * @param params  参数
   * @param operation     接口
   */
  public postData(operation: string, params?: object) {
    return new Promise((resolve, reject) => {
      this.axios
        .post(operation, params)
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err.message);
        });
    });
  }
  public postDataFile(operation: string, params?: object) {
    return new Promise((resolve, reject) => {
      this.axios
        .post(operation, {
          params,
          responseType: "blob"
        })
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err.message);
        });
    });
  }
  public putData(operation: string, params?: object) {
    return new Promise((resolve, reject) => {
      this.axios
        .put(operation, params)
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err.message);
        });
    });
  }
}
let newHttpService = new HttpService();

export { newHttpService, basicIp, wsIp, saomaIp };
